//单例模式  -- 单例代理
import EventSubscrible from "./eventSubscrible.js";
function singleton(className) {
  let instance = null;
  return new Proxy(className, {
    construct(target, argArray, newTarget) {
      if (!instance) {
        //实例不存在，创建
        instance = new target(...argArray);
      }

      return instance;
    },
  });
}

const EventSubscribeSingleton = singleton(EventSubscrible);
export default new EventSubscribeSingleton("发布订阅");

//单例模式 --- 单例工厂

class singletonFactory {
  constructor() {
    this.instance_ = new Map();
  }

  createSingletion(className, ...args) {
    const key = className;
    //实例不存在，创建并保存缓存
    if (!this.instance_.has(key)) {
      const instance = new className(...args);
      this.instance_.set(key, instance);
    }
    //存在，直接返回缓存实例
    return this.instance_.get(key);
  }
}
const sFactory = new singletonFactory();

//export default sFactory.createSingletion(EventSubscrible)
